using System;
using System.Threading;
using System.Windows;
using jSLCore.Client.Presentation.jBinding;
using jSLCore.Client.Utility;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.DomainServices;

namespace jSLCore.Client.Presentation.Wpf.TelerikSupport.Controls
{
    public class jRadDomainDataSource : RadDomainDataSource
    {
        public jRadDomainDataSource()
        {
            
        }

        private BaseControl.DataControl _baseControl
        {
            get
            {
                return UIHelper.FindAncestor<BaseControl.DataControl>(this);
            }
        }

        protected override void OnLoadingData(LoadingDataEventArgs args)
        {
            if(_baseControl.ParentApplicationContainer!=null)
            _baseControl.DoWork(LoadingText,(sender, eventArgs) =>
                                                {
                                                    var cancel = false;
                                                    while(!cancel)
                                                    {
                                                        Dispatcher.BeginInvoke(() =>
                                                                                   {
                                                                                       if(!IsBusy)
                                                                                       {
                                                                                           cancel = true;
                                                                                       }
                                                                                   });
                                                        Thread.Sleep(200);
                                                    }
                                                },(o, completedEventArgs) =>
                                                      {
                                                          
                                                      });
            base.OnLoadingData(args);
        }

        protected override void OnLoadedData(LoadedDataEventArgs args)
        {
            if(args.HasError)
            {
                var exception = args.Error;
                _baseControl.DoWork(true,(sender, eventArgs) =>
                                             {
                                                 throw (Exception)eventArgs.Argument;
                                             },(o, completedEventArgs) =>
                                                                        {
                                                                            if(completedEventArgs.Error==null)
                                                                            {
                                                                                
                                                                            }
                                                                        },exception);
                args.MarkErrorAsHandled();
            }
            base.OnLoadedData(args);            
        }

        public string LoadingText { get; set; }
    }
}